<template>
    <div>
        <!-- 搜索 -->
        <div class="search">
            <back-to></back-to>
            <input type="text" class="search-input" placeholder="请输入商品名称" v-model="searchText" />
            <div class="search-button" @click="fetchshoppings(searchText)"></div>
        </div>
        <!-- 搜索反馈 -->
        <van-empty v-if="!goodsList.length" description="暂无搜索结果" />

        <!-- 商品列表 -->
        <div class="goods-list" v-for="item in goodsList" :key="item.id" @click="toDetail(item.body.product_id)">
            <router-link to="/detail">
                <div class="left">
                    <img :src="item.body.image" alt="">
                </div>
                <div class="right">
                    <h5>{{ item.body.name }}</h5>
                    <span class="describe">{{ item.body.desc | setContent }}</span>
                    <div class="configuration">
                        <div v-for="(param, index) in item.body.class_parameters.list.slice(0, 3)" :key="index">
                            <p>{{ param.name }}</p>
                            <span>{{ param.value | setpeizhi }}</span>
                        </div>
                    </div>
                    <div class="price">
                        <span>¥ {{ item.body.price }}</span>
                        <div v-if="item.body.sale_label.activity_label.length">
                            {{ item.body.sale_label.activity_label[0].type_desc }}
                        </div>
                    </div>
                </div>
            </router-link>
        </div>

        <div class="hot_search" v-if="!goodsList.length">
            <p class="hot_title">热门搜索</p>
            <ul class="hot_name_wrap">
                <li class="hot_item" v-for="item in arr" :key="item.id" @click="btn(item.name)">
                    {{ item.name }}
                </li>
            </ul>
        </div>

    </div>
</template>

<script>
import BackTo from '@/components/BackTo.vue'
import { hotsearch, search } from '@/api/index'
export default {
    data() {
        return {
            // 搜索文本
            searchText: '',
            // 热搜词数组
            arr: [],
            // 商品列表
            goodsList: [],
            // 防抖计时器
            timer: null
        }
    },
    components: {
        BackTo
    },
    filters: {
        setContent(value) {
            return value.slice(0, 37) + '...'
        },
        setpeizhi(value) {
            return value.slice(0, 6) + '..'
        }
    },
    async created() {
        try {
            const content = await hotsearch();
            const { code, data: { hot_class } } = content;

            if (code === 0) {
                this.arr = hot_class.slice(0, 9);
            }
        } catch (error) {
            console.error("热门搜索获取失败:", error);
        }
    },
    methods: {
        async btn(str) {
            this.searchText = str;
            await this.fetchshoppings(str);
        },
        // 获取商品列表
        async fetchshoppings(query) {
            if (!query) return; // 如果搜索框为空，直接返回

            try {
                const { code, data: { list_v2 } } = await search({ query });

                if (code === 0) {
                    this.goodsList = list_v2 || [];
                }
            } catch (error) {
                console.error("搜索结果获取失败:", error);
                alert("搜索失败，请检查网络或稍后重试。");
            }
        },
        // 点击商品跳转详情页
        toDetail(id) {
            this.$router.push({
                path: '/detail',
                query: {
                    id
                }
            });
            console.log(id);
        }
    },
    watch: {
        searchText(val) {
            if (this.timer) clearTimeout(this.timer);

            if (!val) {
                this.goodsList = []; // 搜索框为空，清空搜索结果
                return;
            }

            // 防抖处理
            this.timer = setTimeout(() => {
                this.fetchshoppings(val);
            }, 500); // 500ms内输入内容不再请求接口
        }
    }
}
</script>

<style lang="less" scoped>
.search {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    background-color: rgba(153, 153, 153, 0.2);
    border-bottom: 1px solid #eee;

    .search-input {
        width: 75%;
        height: 25px;
        border: none;
        outline: none;
        padding-left: 10px;
        font-size: 14px;
    }

    .search-button {
        width: 8%;
        height: 30px;
        border: none;
        outline: none;
        color: #fff;
        cursor: pointer;
        background-image: url(../assets/img/search.png);
        background-size: 100% 100%;
    }
}

.goods-list {
    height: 200px;

    a {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        border-bottom: 1px solid #eee;
        justify-content: space-between;

        .left {
            width: 200px;
            height: 200px;
            margin-right: 10px;

            img {
                width: 100%;
                height: 100%;
            }
        }

        .right {
            padding: 15px 0 0 0;
            width: 70%;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-between;

            h5 {
                font-size: 16px;
                margin-bottom: 5px;
                padding-right: 5px;
            }

            .describe {
                font-size: 10px;
                color: #999;
                margin-bottom: 5px;
            }

            .configuration {
                display: flex;
                justify-content: space-around;
                align-items: center;
                text-align: center;
                margin-bottom: 15px;

                >div {
                    height: 25px;

                    p {
                        margin-right: 5px;
                        font-size: 10px;
                    }

                    span {
                        font-size: 10px;
                        color: #333;
                    }
                }
            }

            .price {
                display: flex;
                margin-bottom: 5px;

                span {
                    font-size: 18px;
                    color: #ff5a00;
                }

                div {
                    line-height: 20px;
                    border-radius: 2px;
                    text-align: center;
                    font-size: 11px;
                    background-color: red;
                    color: #fff;
                    margin-left: 10px;
                    padding: 0px 5px;
                }
            }

        }
    }
}

.hot_search {
    padding: 10px;

    .hot_title {
        font-size: 16px;
        color: #666;
    }

    .hot_name_wrap {
        margin: 10px 0;

        .hot_item {
            display: inline-block;
            margin: 5px 10px;
            font-size: 14px;
            height: 30px;
            line-height: 30px;
            border-radius: 20px;
            padding: 0 10px;
            background: #f2f2f2;
            border: 1px solid #d3d4da;
        }
    }
}

.search-icon {
    font-size: 24px;
}
</style>
